package com.richfit.barcodesystem_cczt.activitis;

import android.content.Intent;
import android.os.Bundle;

import com.google.gson.reflect.TypeToken;
import com.richfit.barcodesystem_cczt.activitis.adapters.Detail_Adapter;
import com.richfit.barcodesystem_cczt.activitis.base.BaseActivity;
import com.richfit.barcodesystem_cczt.activitis.model.base.Result;
import com.richfit.barcodesystem_cczt.databinding.ActivityDetailBinding;
import com.richfit.barcodesystem_cczt.fragments.model.ZBoxEntity;
import com.richfit.barcodesystem_cczt.network.OkHttpUtils;
import com.richfit.barcodesystem_cczt.utils.Urls;
import com.richfit.barcodesystem_cczt.utils.dialog.DialogHelper;

import java.util.List;

public class Detail extends BaseActivity {
    private ActivityDetailBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDetailBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
        initData();
    }

    private void initView() {
        binding.icdTop.IVBack.setOnClickListener(v -> finish());
    }

    private void initData() {
        //接受传递的参数
        Intent intent = getIntent();
        String boxId = intent.getStringExtra("boxId");
        String boxNo = intent.getStringExtra("boxNo");
        binding.icdTop.TxtTitle.setText(boxNo);
        //查询箱子信息明细列表
        getBoxDetail(boxId);
    }

    private void getBoxDetail(String boxId) {
        DialogHelper dialogHelper = new DialogHelper(this);
        dialogHelper.showLoadingDialog("正在获取装箱单详情信息");
        OkHttpUtils okHttpUtils = OkHttpUtils.getInstance();
        okHttpUtils.doGet(Urls.URL_ZBOX_DETAIL + "?zboxLineId=" + boxId, bo.cookie, new OkHttpUtils.NetCallBack() {
            @Override
            public void onSuccess(Result result) {
                dialogHelper.dismissDialog();
                String data = gson.toJson(result.getData());
                List<ZBoxEntity.Detail> details = gson.fromJson(data, new TypeToken<List<ZBoxEntity.Detail>>() {
                }.getType());
                Detail_Adapter detail_adapter = new Detail_Adapter(Detail.this, details);
                binding.LVDetail.setAdapter(detail_adapter);
            }

            @Override
            public void onFailure(Exception e) {
                dialogHelper.dismissDialog();
            }
        });
    }
}